BodyParser : Web body parser
Parse incoming request bodies in a middleware before your handlers, available under the req.body property.
This module provides the following parsers:
- JSON body parser
- Raw body parser
- Text body parser
- URL-encoded form body parser
The bodyParser object exposes various factories to create middlewares. All middlewares will populate the req.body property with the parsed body when the Content-Type request header matches the type option, or an empty object ({}) if there was no body to parse, the Content-Type was not matched, or an error occurred.
User can use the following code to import the bodyParser module.
var bodyParser = require('middleware').bodyParser;
Support
The following shows bodyParser module APIs available for each permissions.
| User Mode | Privilege Mode | |
|---|---|---|
| bodyParser.json | ● | ● |
| bodyParser.raw | ● | ● |
| bodyParser.text | ● | ● |
| bodyParser.urlencoded | ● | ● |
BodyParser Object
bodyParser.json([options])
options{Object} contain any of the following keys:limit{Number | String} Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a string, the value is passed to the bytes library for parsing. default:'100kb'.strict{Boolean} When set totrue, will only accept arrays and objects; whenfalsewill accept anythingJSON.parseaccepts. Defaults totrue.reviverThereviveroption is passed directly toJSON.parseas the second argument.type{String | Array | Function} Thetypeoption is used to determine what media type the middleware will parse. This option can be a string, array of strings, or a function. If not a function,typeoption is passed directly to thetype-islibrary and this can be an extension name (likejson), a mime type (likeapplication/json), or a mime type with a wildcard (like*/*or*/json). If a function, thetypeoption is called asfn(req)and the request is parsed if it returns a truthy value. default:'application/json'.
- Returns middleware that only parses
jsonand only looks at requests where theContent-Typeheader matches thetypeoption.
A new body object containing the parsed data is populated on the requestobject after the middleware (i.e. req.body).
bodyParser.raw([options])
options{Object} Therawfunction takes an optionaloptionsobject that may contain any of the following keys:limit{Number | String} Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a string, the value is passed to the bytes library for parsing. default:'100kb'.type{String | Array | Function} Thetypeoption is used to determine what media type the middleware will parse. This option can be a string, array of strings, or a function. If not a function,typeoption is passed directly to thetype-islibrary and this can be an extension name (likebin), a mime type (likeapplication/octet-stream), or a mime type with a wildcard (like*/*orapplication/*). If a function, thetypeoption is called asfn(req)and the request is parsed if it returns a truthy value. default:'application/octet-stream'.
- Returns middleware that parses all bodies as a
Bufferand only looks at requests where theContent Typeheader matches thetypeoption.
A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body). This will be a Buffer object of the body.
bodyParser.text([options])
options{Object} Thetextfunction takes an optionaloptionsobject that may contain any of the following keys:limit{Number | String} Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a string, the value is passed to the bytes library for parsing. default:'100kb'.type{String | Array | Function} Thetypeoption is used to determine what media type the middleware will parse. This option can be a string, array of strings, or a function. If not a function,typeoption is passed directly to thetype-islibrary and this can be an extension name (liketxt), a mime type (liketext/plain), or a mime type with a wildcard (like*/*ortext/*). If a function, thetypeoption is called asfn(req)and the request is parsed if it returns a truthy value. default:'text/plain'.
- Returns middleware that parses all bodies as a string and only looks at requests where the
Content-Typeheader matches thetypeoption.
A new body string containing the parsed data is populated on the requestobject after the middleware (i.e. req.body). This will be a string of the body.
bodyParser.urlencoded([options])
options{Object} Thetextfunction takes an optionaloptionsobject that may contain any of the following keys:limit{Number | String} Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a string, the value is passed to the bytes library for parsing. default:'100kb'.type{String|Array|Function} Thetypeoption is used to determine what media type the middleware will parse. This option can be a string, array of strings, or a function. If not a function,typeoption is passed directly to thetype-islibrary and this can be an extension name (likeurlencoded), a mime type (likeapplication/x-www-form-urlencoded), or a mime type with a wildcard (like*/x-www-form-urlencoded). If a function, thetypeoption is called asfn(req)and the request is parsed if it returns a truthy value. default:'application/x-www-form-urlencoded'.parameterLimit{Integer} TheparameterLimitoption controls the maximum number of parameters that are allowed in the URL-encoded data. If a request contains more parameters than this value, a 413 will be returned to the client. default:1000.
- Returns middleware that only parses
urlencodedbodies and only looks at requests where theContent-Typeheader matches thetypeoption.
A new body string containing the parsed data is populated on the requestobject after the middleware (i.e. req.body). This object will contain key-value pairs, where the value can be a string or array (when extended is false), or any type (when extended is true).
Examples
- This example demonstrates adding a generic JSON and URL-encoded parser as a btop-level middleware, which will parse the bodies of all incoming requests. This is the simplest setup.
var socket = require('socket');
var WebApp = require('webapp');
var iosched = require('iosched');
var bodyParser = require('middleware').bodyParser;
// Create app.
var app = WebApp.create('app', 0, socket.sockaddr(socket.INADDR_ANY, 8000));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded());
// parse application/json
app.use(bodyParser.json());
app.use(function (req, res) {
res.setHeader('Content-Type', 'text/plain');
res.write('you posted:\n');
res.end(JSON.stringify(req.body, null, 2));
});
// Event loop.
while (true) {
iosched.poll();
}
- All the parsers accept a
typeoption which allows you to change theContent-Typethat the middleware will parse.
var socket = require('socket');
var WebApp = require('webapp');
var bodyParser = require('middleware').bodyParser;
// Create app.
var app = WebApp.create('app', 0, socket.sockaddr(socket.INADDR_ANY, 8000));
// parse various different custom JSON types as JSON
app.use(bodyParser.json({ type: 'application/*+json' }));
// parse some custom thing into a Buffer
app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }));
// parse an HTML body into a string
app.use(bodyParser.text({ type: 'text/html' }));




陕公网安备61019002002605号